package cn.stylefeng.guns.modular.tool.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.model.params.BasicGoodsParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicGoodsResult;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicGoodsService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.storage.model.params.StorageStockParam;
import cn.stylefeng.guns.modular.storage.model.result.StorageStockResult;
import cn.stylefeng.guns.modular.storage.service.StorageStockService;
import cn.stylefeng.guns.modular.tool.entity.ToolExpressSumTask;
import cn.stylefeng.guns.modular.tool.entity.ToolTaskArrange;
import cn.stylefeng.guns.modular.tool.entity.ToolWave;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressExcelParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressSumTaskParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolInstallParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolTaskArrangeParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolTaskMoneyParam;
import cn.stylefeng.guns.modular.tool.model.result.ToolExpressExcelResult;
import cn.stylefeng.guns.modular.tool.model.result.ToolExpressSumTaskResult;
import cn.stylefeng.guns.modular.tool.model.result.ToolInstallResult;
import cn.stylefeng.guns.modular.tool.model.result.ToolTaskArrangeResult;
import cn.stylefeng.guns.modular.tool.service.ToolExpressExcelService;
import cn.stylefeng.guns.modular.tool.service.ToolExpressSumTaskService;
import cn.stylefeng.guns.modular.tool.service.ToolInstallService;
import cn.stylefeng.guns.modular.tool.service.ToolStorageStockService;
import cn.stylefeng.guns.modular.tool.service.ToolTaskArrangeService;
import cn.stylefeng.guns.modular.tool.service.ToolTaskMoneyService;
import cn.stylefeng.guns.modular.tool.service.ToolWaveService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;


/**
 * 作业统计管理控制器
 */
@Controller
@RequestMapping("/toolExpressSumTask")
public class ToolExpressSumTaskController extends BaseController {

    private String PREFIX = "/tool/toolExpressSumTask";

    @Autowired
    private ToolExpressSumTaskService toolExpressSumTaskService;

    @Autowired
    private BasicCustomerService basicCustomerService;

    @Autowired
    private UserService userService;

    @Autowired
    private ToolTaskArrangeService toolTaskArrangeService;
    
    private Lock lock = new ReentrantLock();

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/toolExpressSumTask.html";
    }

    /**
     * 跳转到主页面
     */
    @RequestMapping("toolExpressSumTask2")
    public String toolExpressSumTask2(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/toolExpressSumTask2.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add(Model model,Long waveId) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	
    	ToolTaskArrangeParam toolTaskArrangeParam = new ToolTaskArrangeParam();
    	toolTaskArrangeParam.setWaveId(waveId);
    	List<ToolTaskArrangeResult> alist = toolTaskArrangeService.findListBySpec(toolTaskArrangeParam);
    	model.addAttribute("alist", alist);
        return PREFIX + "/toolExpressSumTask_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/toolExpressSumTask_edit.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ToolExpressSumTaskParam toolExpressSumTaskParam) {
    	BasicCustomer basicCustomer = basicCustomerService.getById(toolExpressSumTaskParam.getCustomerId());
    	toolExpressSumTaskParam.setCustomerName(basicCustomer.getCustomerName());

    	ToolTaskArrange toolTaskArrange = toolTaskArrangeService.getById(toolExpressSumTaskParam.getArrangeId());
    	toolExpressSumTaskParam.setArrangeNumber(toolTaskArrange.getGroupNumber());
    	toolExpressSumTaskParam.setUserName(toolTaskArrange.getGroupUsers());
    	
    	BigDecimal num = new BigDecimal(toolExpressSumTaskParam.getNum());
    	BigDecimal sumNum = new BigDecimal(toolExpressSumTaskParam.getSumNum());
    	BigDecimal v1 = num.multiply(sumNum).setScale(2, BigDecimal.ROUND_HALF_DOWN);
    	toolExpressSumTaskParam.setV1(v1.toString());//作业量
    	toolExpressSumTaskParam.setDeptId(userService.getDeptId());
        this.toolExpressSumTaskService.add(toolExpressSumTaskParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ToolExpressSumTaskParam toolExpressSumTaskParam) {
        this.toolExpressSumTaskService.update(toolExpressSumTaskParam);
        return ResponseData.success();
    }

    /**
     * 批量删除接口
     */
    @RequestMapping("/btnRevoke")
    @ResponseBody
    public ResponseData btnRevoke(Integer[] ids) {
    	for (Integer id : ids) {
    		ToolExpressSumTaskParam toolExpressSumTaskParam = new ToolExpressSumTaskParam();
    		toolExpressSumTaskParam.setId(id);
    		this.toolExpressSumTaskService.delete(toolExpressSumTaskParam);
		}
        return ResponseData.success();
    }

    /**
     * 清空作业接口
     */
    @RequestMapping("/deletetask")
    @ResponseBody
    public ResponseData deletetask(Long waveId) {
    	ToolExpressSumTaskParam expressSumTaskParam = new ToolExpressSumTaskParam();
	    expressSumTaskParam.setWaveId(waveId);
	    toolExpressSumTaskService.deleteBySpec(expressSumTaskParam);
	    
    	ToolTaskMoneyParam moneyParam = new ToolTaskMoneyParam();
    	moneyParam.setWaveId(waveId);
    	toolTaskMoneyService.deleteBySpec(moneyParam);//删除工资明细
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ToolExpressSumTaskParam toolExpressSumTaskParam) {
        ToolExpressSumTask detail = this.toolExpressSumTaskService.getById(toolExpressSumTaskParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表 统计
     */
    @ResponseBody
    @RequestMapping("/listtj")
    public LayuiPageInfo listtj(ToolExpressSumTaskParam toolExpressSumTaskParam,String condition,String startTime,String endTime ) {
    	User user = userService.getcurrentUser();
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		toolExpressSumTaskParam.setV6("'"+startTime+"' and '"+endTime+"'");
		} else if (startTime == null && endTime == null){
			startTime = DateUtil.getDate();
			endTime = DateUtil.getDate();
			toolExpressSumTaskParam.setV6("'"+startTime+"' and '"+endTime+"'");
		}
    	toolExpressSumTaskParam.setDeptId(user.getDeptId());
    	LayuiPageInfo LayuiPageInfo = this.toolExpressSumTaskService.findPageBySpectj(toolExpressSumTaskParam,condition,user.getOperateId());
        return LayuiPageInfo;
    }

    /**
     * 查询列表 统计
     * 组合数汇总
     */
    @ResponseBody
    @RequestMapping("/listtjnum")
    public LayuiPageInfo listtjnum(ToolExpressSumTaskParam toolExpressSumTaskParam,String condition,String startTime,String endTime ) {
    	User user = userService.getcurrentUser();
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		toolExpressSumTaskParam.setV6("'"+startTime+"' and '"+endTime+"'");
		} else if (startTime == null && endTime == null){
			startTime = DateUtil.getDate();
			endTime = DateUtil.getDate();
			toolExpressSumTaskParam.setV6("'"+startTime+"' and '"+endTime+"'");
		}
    	toolExpressSumTaskParam.setDeptId(user.getDeptId());
    	LayuiPageInfo LayuiPageInfo = this.toolExpressSumTaskService.findPageBySpectjnum(toolExpressSumTaskParam,condition,user.getOperateId());
        return LayuiPageInfo;
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ToolExpressSumTaskParam toolExpressSumTaskParam,String condition ) {
    	LayuiPageInfo LayuiPageInfo = this.toolExpressSumTaskService.findPageBySpec(toolExpressSumTaskParam,condition);
        return LayuiPageInfo;
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/listd")
    public LayuiPageInfo listd(ToolExpressSumTaskParam toolExpressSumTaskParam,String condition ) {
    	LayuiPageInfo LayuiPageInfo = this.toolExpressSumTaskService.findPageBySpecd(toolExpressSumTaskParam,condition);
        return LayuiPageInfo;
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/listSum")
    public LayuiPageInfo listSum(ToolExpressSumTaskParam toolExpressSumTaskParam,String condition ) {
        return this.toolExpressSumTaskService.findPageSumBySpec(toolExpressSumTaskParam,condition);
    }

    /**
     * 查询消耗库存明细列表
     */
    @ResponseBody
    @RequestMapping("/listtjnumss")
    public LayuiPageInfo listtjnumss(ToolExpressExcelParam toolExpressExcelParam,String condition,String startTime,String endTime) {
    	User user = userService.getcurrentUser();
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		toolExpressExcelParam.setV8("'"+startTime+"' and '"+endTime+"'");
		} else if (startTime == null && endTime == null){
			startTime = DateUtil.getDate();
			endTime = DateUtil.getDate();
			toolExpressExcelParam.setV8("'"+startTime+"' and '"+endTime+"'");
		}
    	toolExpressExcelParam.setDeptId(user.getDeptId());
        return this.toolExpressExcelService.findPageTjnumssBySpec(toolExpressExcelParam,condition,user.getOperateId());
    }

    /**
     * 查询消耗库存汇总列表
     */
    @ResponseBody
    @RequestMapping("/listtjnumssSum")
    public LayuiPageInfo listtjnumssSum(ToolExpressExcelParam toolExpressExcelParam,String condition,String startTime,String endTime) {
    	User user = userService.getcurrentUser();
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		toolExpressExcelParam.setV8("'"+startTime+"' and '"+endTime+"'");
		} else if (startTime == null && endTime == null){
			startTime = DateUtil.getDate();
			endTime = DateUtil.getDate();
			toolExpressExcelParam.setV8("'"+startTime+"' and '"+endTime+"'");
		}
    	toolExpressExcelParam.setDeptId(user.getDeptId());
        return this.toolExpressExcelService.findPageTjnumssSumBySpec(toolExpressExcelParam,condition,user.getOperateId());
    }

    @Autowired
    private ToolTaskMoneyService toolTaskMoneyService;

    @Autowired
    private ToolExpressExcelService toolExpressExcelService;

    /**
     * 更新系统库存
     */
    @RequestMapping("/updateStock")
    @ResponseBody
    public ResponseData updateStock(ToolExpressExcelParam toolExpressExcelParam,Long outStorehouseId) {
    	User user = userService.getcurrentUser();
    	ResponseData data = new ResponseData();
		lock.lock();//获取锁
		try {
	    	data = toolExpressSumTaskService.updateStock(toolExpressExcelParam,outStorehouseId,user);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();//释放锁
		}
		return data;
    }

    /**
     * 生成工资接口
     */
    @RequestMapping("/batchassign")
    @ResponseBody
    public ResponseData batchassign(ToolExpressSumTaskParam toolExpressSumTaskParam) {
    	ResponseData data = new ResponseData();
    	User user = userService.getcurrentUser();

		lock.lock();//获取锁
		try {
	    	data = toolExpressSumTaskService.batchassign(toolExpressSumTaskParam,user);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();//释放锁
		}
		return data;
    }
    
    public static void main(String[] args) {
		String str = "NX8020*2，NX8108*4，NX8207*2，NX8805*2，";
		String[] strs = str.split("，");
		//System.out.println(strs.length);
	}

}


